package com.lovo.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.project.bean.JournalBean;
import com.lovo.project.bean.JurisdictionBean;
import com.lovo.project.bean.RoleBean;
import com.lovo.project.bean.UserBean;
import com.lovo.project.mapper.UserMapper;
import com.lovo.project.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/queryRoleList")
    public List<RoleBean> queryRoleList() {
        List<RoleBean> list = roleService.selectRoleList();
        return list;
    }

    @RequestMapping("/queryUserRoleList")
    public List<Integer> queryUserRoleList(Integer userId) {
        List<RoleBean> list = roleService.queryUserRoleList(userId);
        List<Integer> list1=new ArrayList<>();
        for (RoleBean b:list){
            list1.add(b.getRoleId());
        }
        return list1;
    }


    @RequestMapping("/updateUserRole")
    public String updateUserRole(@RequestParam("userId") Integer userId,@RequestParam("roleId") List<Integer> roleId) {
        roleService.updateUserRole(userId, roleId);
        return "ok";
    }

    @RequestMapping("/deleteRole")
    public String deleteRole(Integer roleId) {
       roleService.deleteRole(roleId);
       return "ok";
    }
    @RequestMapping("/queryRoleList1")
    public Page<RoleBean> queryRoleList1(Integer pg,Integer pgsize) {
        Page<RoleBean> page=new Page<>(pg,pgsize);
        Page<RoleBean> pageInfo = roleService.selectRoleList1(page,null);
        return pageInfo;
    }
    @RequestMapping("/queryRoleNameAndjurisdictionList")
    public RoleBean queryRoleNameAndjurisdictionList(Integer roleId) {
        RoleBean roleBean=roleService.queryRole(roleId);
        return roleBean;
    }
}
